﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Nini.Config;
using System.Windows.Forms;

namespace PowerControl
{
    public class BaseStation : Positionable
    {
        private Antenna antenna;

        public BaseStation()
        {
            this.antenna = new BaseStationAntenna();
        }

        public Antenna Antenna
        {
            get
            {
                return antenna;
            }
            set
            {
                antenna = value;
            }
        }

        public Position CurrentPosition
        {
            get
            {
                return this.Antenna.Position;
            }
            set
            {
                this.Antenna.Position = value;
            }
        }

        public static BaseStation FromXMLNode(XmlNode n)
        {
            BaseStation b = new BaseStation();
            //Retrieve base station model
            String bs_model = n.Attributes.GetNamedItem("model").Value;
            //Load model values from ini file
            IniConfigSource source = new IniConfigSource(Application.StartupPath + "\\config\\" + bs_model);
            b.Antenna.TransmittingPower = Utils.dBToWatt(source.Configs[0].GetDouble("maxpower"))/1000.0;
            b.Antenna.Sensibility = source.Configs[0].GetDouble("sensibility");
            b.Antenna.Gain = source.Configs[0].GetDouble("antennagain");
            //

            for (int i = 0; i < n.ChildNodes.Count; ++i)
            {
                XmlNode binfonode = n.ChildNodes.Item(i);

                //We have position information
                if (binfonode.Name == "position")
                {
                    for (int j = 0; j < binfonode.ChildNodes.Count; ++j)
                    {
                        XmlNode posinfonode = binfonode.ChildNodes.Item(j);
                        switch (posinfonode.Name)
                        {
                            case "x":
                                b.Position.X = Double.Parse(posinfonode.InnerText);
                                break;
                            case "y":
                                b.Position.Y = Double.Parse(posinfonode.InnerText);
                                break;
                            case "z":
                                b.Position.Z = Double.Parse(posinfonode.InnerText);
                                break;
                            case "pitch":
                                b.Position.Pitch = Double.Parse(posinfonode.InnerText);
                                break;
                            case "yaw":
                                b.Position.Yaw = Double.Parse(posinfonode.InnerText);
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            return b;
        }
    }
}
